<?php
namespace app\controller;

use app\BaseController;
use app\model\User;

class Oauth extends BaseController
{
    //登录地址
    public function index($type = null)
    {
        if ($type == null) {
            dump('参数错误');exit;
        }

        session('returnUrl',$_SERVER['HTTP_REFERER']);

        // 获取对象实例
        $sns = \liliuwei\social\Oauth::getInstance($type);
        //跳转到授权页面
        return redirect($sns->getRequestCodeURL());
    }

    //授权回调地址
    public function callback($type = null, $code = null)
    {
        if ($type == null || $code == null) {
            dump('参数错误');exit;
        }
        $sns = \liliuwei\social\Oauth::getInstance($type);
        // 获取TOKEN
        $token = $sns->getAccessToken($code);
        //获取当前第三方登录用户信息
        if (is_array($token)) {

            $user_info = \liliuwei\social\GetInfo::getInstance($type, $token);
            // dump($user_info);// 获取第三方用户资料
            // $sns->openid();//统一使用$sns->openid()获取openid
            // $sns->unionid();//QQ和微信、淘宝可以获取unionid

            $user = User::where('openid', $sns->openid())->find();
            if($user){
                // $user->type         = $user_info['type'];
                // $user->name         = $user_info['name'];
                // $user->nickname     = $user_info['nickname'];
                // $user->avatar       = $user_info['avatar'];
                // $user->gender       = $user_info['gender'];
                $user->update_time  = time();
                $user->save();

                $user_info = array(
                    'id'       => $user['id'],
                    'type'     => $user['type'],
                    'name'     => $user['name'],
                    'nickname' => $user['nickname'],
                    'avatar'   => $user['avatar'],
                );

            } else{

                // 区分登录类型
                
                switch ($type) {
                    case 'qq':
                        $arr = array(
                            'type'          => $user_info['type'],
                            'name'          => $user_info['name'],
                            'nickname'      => $user_info['nickname'],
                            'avatar'        => $user_info['avatar'],
                            'gender'        => $user_info['gender'],
                            'openid'        => $sns->openid(),
                            'unionid'       => $sns->unionid(),
                            'create_time'   => time(),
                            'update_time'   => time(),
                        );
                        break;
                    
                    case 'gitee':
                        $arr = array(
                            'type'          => $user_info['type'],
                            'name'          => $user_info['name'],
                            'nickname'      => $user_info['nickname'],
                            'avatar'        => $user_info['avatar'],
                            'html_url'      => $user_info['html_url'],
                            'openid'        => $sns->openid(),
                            'create_time'   => time(),
                            'update_time'   => time(),
                        );
                        break;
                }

                $res = User::create($arr);

                $user_info = array(
                    'id'       => $res['id'],
                    'type'     => $res['type'],
                    'name'     => $res['name'],
                    'nickname' => $res['nickname'],
                    'avatar'   => $res['avatar'],
                );
            }

            session('zbdInfo', $user_info);

            $returnUrl = session('returnUrl');

            session('returnUrl',null);

            return redirect($returnUrl ?? '/');

            // return redirect('/');


        } else {
            echo "获取第三方用户的基本信息失败";
        }
    }
}